/*
 *  This file is part of ZiggyStardust.
 *
 *  ZiggyStardust is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  ZiggyStardust is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.wisc.csvParser.plugins;

/**
 *
 * @author  user
 */
public class JDialogDateParserCSIArray extends java.awt.Dialog {
    
    DateTimeParserCSI parser;
    
    /** Creates new form JDialogDateParserCSIArray */
    public JDialogDateParserCSIArray(java.awt.Frame parent, boolean modal,DateTimeParserCSI parser) {
        super(parent, modal);
        initComponents();
        
        this.parser = parser;
        
        yearTF.setText(Integer.toString(parser.getYearIndex()));
        daynumTF.setText(Integer.toString(parser.getDaynumIndex()));
        
        
        if(parser.getTimeIndex() > -1){
            hasTimeCB.setSelected(true);
            timeTF.setEnabled(true);
            timeTF.setText(Integer.toString(parser.getTimeIndex()));
        }else{
            timeTF.setText("");
        }
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        asdfasdf = new javax.swing.JLabel();
        yearTF = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        daynumTF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        timeTF = new javax.swing.JTextField();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        hasTimeCB = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        asdfasdf.setText("Year Column Number:");

        yearTF.setText("1");

        jLabel1.setText("Daynum Column Number:");

        daynumTF.setText("2");

        jLabel2.setText("Time Column Number:");

        timeTF.setText("3");
        timeTF.setEnabled(false);

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        hasTimeCB.setText("Has Time");
        hasTimeCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hasTimeCBActionPerformed(evt);
            }
        });
        hasTimeCB.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                hasTimeCBPropertyChange(evt);
            }
        });

        jLabel3.setText("<html>IMPORTANT: All column indexes are zero indexed. This<br />\nmeans the first column is column 0, the second is column 1,<br />\netc.</html>\n");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel1)
                                .addComponent(asdfasdf))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(timeTF)
                                .addComponent(yearTF)
                                .addComponent(daynumTF, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(cancelButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(hasTimeCB)
                            .addComponent(saveButton)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3)))
                .addContainerGap(11, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(asdfasdf)
                    .addComponent(yearTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(daynumTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timeTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hasTimeCB)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        int yearIndex = -1;
        int daynumIndex = -1;
        int timeIndex = -1;
        
        
        try{
            yearIndex = Integer.parseInt(yearTF.getText());
            daynumIndex = Integer.parseInt(daynumTF.getText());
            if(hasTimeCB.isSelected()){
                timeIndex = Integer.parseInt(timeTF.getText());
            }
            
        }catch(NumberFormatException e){
            javax.swing.JOptionPane.showMessageDialog(null,"Indexes must be positive integers or zero.");
            return;
        }
        
        if(yearIndex < 0 || daynumIndex < 0 || (timeIndex < 0 && hasTimeCB.isSelected())){
            javax.swing.JOptionPane.showMessageDialog(null,
                    "All indexes must be integers zero or greater.");
            return;
        }
        
        parser.setDaynumIndex(daynumIndex);
        parser.setTimeIndex(timeIndex);
        parser.setYearIndex(yearIndex);
        
        //if we've gotten here, user wants to exit and input is all ok.
        setVisible(false);
        dispose();

        
    }//GEN-LAST:event_saveButtonActionPerformed

    private void hasTimeCBPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_hasTimeCBPropertyChange
        timeTF.setEnabled(hasTimeCB.isSelected());
}//GEN-LAST:event_hasTimeCBPropertyChange

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void hasTimeCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hasTimeCBActionPerformed
        timeTF.setEnabled(hasTimeCB.isSelected());
    }//GEN-LAST:event_hasTimeCBActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel asdfasdf;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField daynumTF;
    private javax.swing.JCheckBox hasTimeCB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton saveButton;
    private javax.swing.JTextField timeTF;
    private javax.swing.JTextField yearTF;
    // End of variables declaration//GEN-END:variables
    
}
